<template>
  <div class="search-panel">
    <el-row class="m-header-searchbar">
      <el-col
        :span="3"
        class="left hidden-xs-only"
      >
        <img
          src="//s0.meituan.net/bs/fe-web-meituan/e5eeaef/img/logo.png"
          alt="美团"
          style="cursor: pointer"
          @click="$router.push('/')"
        >
      </el-col>
      <el-col
        :span="15"
        :xs="{ span: 24 }"
        class="center"
      >
        <div class="wrapper">
          <el-input
            v-model="search"
            placeholder="搜索商家或地点"
            @focus="focus"
            @blur="blur"
            @input="input"
          />
          <button class="el-button el-button--primary" @click="handleSearch">
            <i class="el-icon-search" />
          </button>
          <dl
            v-if="isHotPlace"
            class="hotPlace"
          >
            <dt>热门搜索</dt>
            <dd
              v-for="(item,idx) in $store.state.home.hotPlace.slice(0,5)"
              :key="idx"
            >
              <nuxt-link :to="{path:'detail',query:{keyword:item.name,type:item.type}}">
                {{ item.name }}
              </nuxt-link>
            </dd>
          </dl>
          <dl
            v-if="isSearchList"
            class="searchList"
          >
            <dd
              v-for="(item,idx) in searchList"
              :key="idx"
            >
              <nuxt-link :to="{path:'detail',query:{keyword:item.name,type:item.type}}">
                {{ item.name }}
              </nuxt-link>
            </dd>
          </dl>
        </div>
        <p class="suggest">
          <nuxt-link
            v-for="(item,idx) in suggestList"
            :key="idx"
            :to="'/products?keyword='+encodeURIComponent(item.name)"
          >
            {{ item.name }}
          </nuxt-link>
        </p>
        <ul class="nav">
          <li>
            <nuxt-link
              to="/"
              class="takeout"
            >
              美团外卖
            </nuxt-link>
          </li>
          <li>
            <nuxt-link
              to="/"
              class="movie"
            >
              猫眼电影
            </nuxt-link>
          </li>
          <li>
            <nuxt-link
              to="/"
              class="hotel"
            >
              美团酒店
            </nuxt-link>
          </li>
          <li>
            <nuxt-link
              to="/"
              class="apartment"
            >
              民宿/公寓
            </nuxt-link>
          </li>
          <li>
            <nuxt-link
              to="/"
              class="business"
            >
              商家入驻
            </nuxt-link>
          </li>
        </ul>
      </el-col>
      <el-col
        :span="6"
        class="right hidden-xs-only"
      >
        <ul class="security">
          <li>
            <i class="refund" /><p class="txt">
              随时退
            </p>
          </li>
          <li>
            <i class="single" /><p class="txt">
              不满意免单
            </p>
          </li>
          <li>
            <i class="overdue" /><p class="txt">
              过期退
            </p>
          </li>
        </ul>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import _ from 'lodash'
export default {
  data() {
    return {
      search: '',
      isFocus: false,
      hotPlace: [],
      searchList: [],
      suggestList: [{ name: '丽人' }, { name: '电影演出' }, { name: '美食' }]
    }
  },
  computed: {
    isHotPlace: function () {
      return this.isFocus && !this.search
    },
    isSearchList: function () {
      return this.isFocus && this.search
    }
  },
  methods: {
    focus: function () {
      this.isFocus = true
    },
    blur: function () {
      const self = this
      setTimeout(function () {
        self.isFocus = false
      }, 200)
    },
    input: _.debounce(async function () {
      const self = this
      const city = self.$store.state.geo.position.city
      self.searchList = []
      const { status, data: { pois } } = await self.$axios.get('/search/resultsByName', {
        params: {
          name: self.search,
          city
        }
      })
      if (status) {}
      self.searchList = pois.slice(0, 10)
    }, 300),
    handleSearch() {
      this.$router.push({ path: 'products', query: { keyword: this.search } })
    }
  }
}
</script>

<style lang="css">
</style>
